
//
//  WYCCardlistPickerView.m
//  信用卡管理
//
//  Created by 中企互联 on 2017/11/23.
//  Copyright © 2017年 李胜军. All rights reserved.
//

#import "WYCCardlistPickerView.h"

@interface WYCCardlistPickerView () <UIPickerViewDelegate,UIPickerViewDataSource>


@property (nonatomic,weak) UIToolbar *toolBar;
@property (nonatomic, weak) UIPickerView *pickerView;
/** 数组 */
@property (nonatomic,strong) NSArray *SourceDataArray;
@property (nonatomic,weak) WYCBillModel *selectStrModel;
@end

@implementation WYCCardlistPickerView

- (instancetype)initWithSourceData:(NSArray *)sourceData confirmBlock:(WYCCardlistPickerViewBlock)confirmBlock cancelBlock:(WYCCardlistPickerViewBlock)cancelBlock {
    
    self = [super init];
    if (self) {
        
        self.SourceDataArray = sourceData;
        self.confirmBlock = confirmBlock;
        self.cancelBlock = cancelBlock;
    }
    
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    
    return self;
}
- (void)refreshWithSourceData:(NSArray *)sourceData {
    
    self.SourceDataArray = sourceData;
    [self.pickerView reloadAllComponents];
}

- (void)setupUI {
    
    self.backgroundColor = [UIColor whiteColor];
    
    UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44)];
    _toolBar.barTintColor=[UIColor whiteColor];
    UIBarButtonItem *item1=[[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancelBtnClick:)];
    UIBarButtonItem *item2=[[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(confirmBtnClick:)];
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    toolBar.items = @[item1,spaceItem, item2];
    [self addSubview:toolBar];
    self.toolBar =toolBar;
    
    
    CGFloat W = ScreenWidth;
    CGFloat H = 280 - 44;
    CGFloat X = 0;
    CGFloat Y = 44;
    UIPickerView *pickerView =  [[UIPickerView alloc] initWithFrame:CGRectMake(X, Y, W, H)];
    pickerView.backgroundColor = [UIColor whiteColor];
    pickerView.delegate = self;
    pickerView.dataSource = self;
    pickerView.showsSelectionIndicator = YES;
    [self addSubview:pickerView];
    self.pickerView = pickerView;
    
}



- (void)confirmBtnClick:(id)send {
   
    WYCLogFunc;
    if (self.selectStrModel == nil) {
        if (self.SourceDataArray.count == 0) {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"请添加信用卡"];
            return;
        }else{
         self.confirmBlock(self.SourceDataArray[0]);
            return;
            
        }
    }

    self.confirmBlock(self.selectStrModel);
}
- (void)cancelBtnClick:(id)send {
   
    self.cancelBlock(nil);
}


#pragma mark -------------------------- pickerView delegate ----------------------------------------

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    return [self.SourceDataArray count];
    
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    
    return 30;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    //o列从区域数组中选择
    
    
    
    WYCBillModel *model = self.SourceDataArray[row];
    return model.bank;
//    return self.SourceDataArray[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    if (self.SourceDataArray.count != 0) {
        WYCBillModel *model = self.SourceDataArray[row];
        self.selectStrModel = model;
    }
    
}

@end
